package top.hundreds.oasys.controller.FrontController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import top.hundreds.common.crud.bean.PageParam;
import top.hundreds.common.web.api.ApiResult;
import top.hundreds.oasys.bean.entity.Project;
import top.hundreds.oasys.bean.vo.ProjectInfoVo;
import top.hundreds.oasys.bean.vo.ProjectVo;
import top.hundreds.oasys.service.ProjectService;

import javax.annotation.Resource;

/**
 * (Project)表控制层
 *
 * @author lcf
 * @since 2021-01-29 19:19:05
 */
@RestController
@RequestMapping("/front/project")
public class ProjectController {
    /**
     * 服务对象
     */
    @Resource
    private ProjectService projectService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("get")
    public ApiResult<ProjectInfoVo> get(Long id) {
        Project project = this.projectService.get(id);
        ProjectInfoVo projectInfoVo = this.projectService.toInfoVo(project);
        return ApiResult.success(projectInfoVo);
    }

    @PostMapping("list")
    public ApiResult<Page<ProjectVo>> list(@RequestBody PageParam<Project> pageParam) {
        Page<Project> page = this.projectService.getPage(pageParam);
        Page<ProjectVo> projectVoPage = this.projectService.toVoPage(page);
        return ApiResult.success(projectVoPage);
    }

    @PostMapping("myProject")
    public ApiResult<Page<ProjectVo>> myProject(@RequestBody PageParam<ProjectInfoVo> pageParam, @RequestAttribute("id") Long id) {
        Page<ProjectVo> projectVoPage = this.projectService.myProject(pageParam, id);
        return ApiResult.success();
    }

    @PostMapping("save")
    public ApiResult<String> save(@RequestBody ProjectInfoVo projectInfoVo) {
        if (projectInfoVo.getId() != null) {
            this.projectService.insertVo(projectInfoVo);
        }else {
            this.projectService.updateVo(projectInfoVo);
        }
        return ApiResult.success();
    }

    @DeleteMapping("remove")
    public ApiResult<String> remove(Long id) {
        this.projectService.remove(id);
        return ApiResult.success();
    }

    @GetMapping("check")
    public ApiResult<String> check(Long id) {
        this.projectService.setStatus(id,1);
        return ApiResult.success();
    }

    @GetMapping("finish")
    public ApiResult<String> finish(Long id) {
        this.projectService.setStatus(id,2);
        return ApiResult.success();
    }
}
